package org.vartist.atld.menu.activity.common;

import org.vartist.atld.R;
import org.vartist.atld.menu.activity.main.MainActivity;
import org.vartist.atld.menu.consts.MessageConst;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class CommonBackPressed {

	private boolean mBackPressedStatus = false;
	private Activity mActivity = null;

	public CommonBackPressed(Activity activity) {
		mActivity = activity;
	}
	
	public void onBackPressed() {
		if (mBackPressedStatus) {
			mActivity.finish();
		} else {
			if (mActivity instanceof MainActivity) {
				mBackPressedStatus = true;
				mHandler.sendMessageDelayed(mHandler.obtainMessage(MessageConst.RESET_BACK_PRESSED_STATE_MSG), 5000); 
				Toast.makeText(mActivity, mActivity.getString(R.string.label_back_pressed_exit), Toast.LENGTH_SHORT).show();				
			} else {
				mActivity.finish();
			}
		}
	}
	
	public void destroy() {
		mHandler.removeCallbacksAndMessages(null);
	}
	
	public Handler mHandler = new Handler(){
		
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			
			switch(msg.what){
				case MessageConst.RESET_BACK_PRESSED_STATE_MSG:
					mBackPressedStatus = false;
					break;
					
				default:
					break;
				}
		}
	};
}
